package ca.nfb.media.nfbPlayer.views
{
	import ca.nfb.media.nfbPlayer.ComModule;
	import ca.nfb.media.nfbPlayer.ui.NFBImage;
	import ca.turbulent.media.Pyro;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.FullScreenEvent;
	import flash.events.MouseEvent;
	import flash.net.*;
	
	public class PreviewImage extends NFBPlayerView
	{
		private var image:NFBImage;
		public var bkgd_mc:MovieClip = new MovieClip();
		
		private var originalWidth:Number;
		private var originalHeight:Number;
		
		public function PreviewImage(w:Number, h:Number)
		{
			super();
			originalWidth = w;
			originalHeight = h;
			
			bkgd_mc.graphics.beginFill(0, 0);
			bkgd_mc.graphics.drawRect(0, 0, w, h);
			bkgd_mc.useHandCursor = true;
			bkgd_mc.mouseEnabled = true;
			bkgd_mc.buttonMode = true;
			bkgd_mc.addEventListener(MouseEvent.CLICK, playMovie, false, 0, true);
		//	TabIndexManager.appendGrouppedTabItems(this.name, [bkgd_mc]); 
			image = new NFBImage(originalWidth, originalHeight);
			image.x = player.x;
			image.y = player.y;
			addChild(image);
			addChild(bkgd_mc);
			image.addEventListener(NFBImage.IMAGE_LOADING, loading, false, 0, true);
			image.addEventListener(NFBImage.IMAGE_LOADED, loaded, false, 0, true);
		}
		
		public function playMovie(evt:MouseEvent=null):void
		{
			if (core._movieID != "" || core.directURL != "" || ComModule.getInstance().latestFileURL != "")
			{
				hideImage();
		 		core.setCurrentState(NFBPlayer.DEFAULT_STATE);	
		 		// player.play("rtmp://flash.onf.ca/onf/Webfilms/filmpop/c_gangbangs_orderly_chaos_en_hv.flv");
				player.play(ComModule.getInstance().latestFileURL);
				// player.play("http://epoirier.developers.turbulent.ca/pyro/sharedmedias/videos/jdjdjdjd.mp4");
			}
		}
		
		public function hideImage():void
		{
			image.clear();
			this.visible = false;
			
			if (core.warningMessageView.visible)
				core.warningMessageView.hideWarningMessage();
		
			if (core.embeddedWelcomeView.visible)
				core.embeddedWelcomeView.hide();
		}
		
		override protected function addedToStage(evt:Event):void 
		{ 
			registerStageEvents();
		}
		
		private function loading(evt:Event):void
		{
			player.forceDispatchPending();
		}
		
		private function loaded(evt:Event):void
		{
			if (!core.embeddedMode && !core.showEmbeddedWelcome) 
				core.bigPlayOverlay.visible = true;
			
			core.bufferingView.stopModal();
			core.bufferingView.visible = false;
		}
			
		public function loadImage(url:String):void
		{
			image.startLoading(url);
		}
		
		override protected function fullscreenHandler(evt:FullScreenEvent):void 
		{
			if (evt.fullScreen)
			{
				image.resize(stage.stageWidth, stage.stageHeight-core.controllerHeight);
			}
			else
			{
				image.resize(originalWidth, originalHeight);
			}
			
			bkgd_mc.width = stage.stageWidth;
			bkgd_mc.height = stage.stageHeight-core.controllerHeight;
		}
		
		override protected function stageResized(evt:Event=null):void
		{
			image.resize(stage.stageWidth, stage.stageHeight-core.controllerHeight);
			bkgd_mc.width = stage.stageWidth;
			bkgd_mc.height = stage.stageHeight-core.controllerHeight;
		}
		
		override protected function coreStateUpdated(evt:Event=null):void 
		{
			if (core.currentState == NFBPlayer.PREVIEW_IMAGE_STATE)
			{
				loadImage(core.previewImageURL);
				this.visible = true;
			}	
			else
			{
				if (core.currentState != NFBPlayer.MESSAGE_STATE)
				{
					this.visible = false;
				}
			}
		}
	}
}